package com.luxuewen.sxa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luxuewen.sxa.common.annotation.ApiAuth;
import com.luxuewen.sxa.entity.SxaUsers;
import com.luxuewen.sxa.entity.response.UserListRes;
import com.luxuewen.sxa.service.impl.SxaUsersServiceImpl;
import com.luxuewen.sxa.utils.response.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author wen
 */
@Slf4j
@RestController
@Validated
public class UserController {
    @Value("${advice}")
    private String advice;

    @Autowired
    private SxaUsersServiceImpl sxaUsersService;

    @GetMapping("/")
    public String index () {
        return advice;
    }

    @ApiAuth(alias = "user:list")
    @GetMapping("user/list/{deptId}/{page}/{limit}")
    public ApiResponse<Page<Map<String, Object>>> list (@PathVariable Integer deptId, @PathVariable long page, @PathVariable long limit) {
        QueryWrapper<SxaUsers> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "username", "avatar", "created_at", "updated_at");
        if (deptId > 0) {
            wrapper.inSql("id", "select user_id from sxa_department_users where department_id = " + deptId);
        }

        Page<Map<String, Object>> ePage = new Page<>(page, limit);
        Page<Map<String, Object>> list = sxaUsersService.pageMaps(ePage, wrapper);

        return ApiResponse.success(list);
    }

//    @PostMapping("user/validate")
//    public ApiResponse<Boolean> validate (@NotNull(message = "用户名不能为空") @RequestParam String username) {
//        QueryWrapper<SxaUsers> wrapper = new QueryWrapper<>();
//        wrapper.eq("username", username);
//
//        boolean res = sxaUsersService.exists(wrapper);
//        if (res) {
//            return ApiResponse.fail("用户名已存在，请更换！");
//        }
//
//
//        return ApiResponse.success(res);
//    }

    @ApiAuth(alias = "user:create")
    @PostMapping("user/create")
    public ApiResponse<SxaUsers> create (@Validated @RequestBody SxaUsers sxaUsers) {
        return ApiResponse.success(sxaUsersService.create(sxaUsers));
    }

    @ApiAuth(alias = "user:update")
    @PostMapping("user/update")
    public ApiResponse<SxaUsers> update (@Validated @RequestBody SxaUsers sxaUsers) {
        return ApiResponse.success(sxaUsersService.update(sxaUsers));
    }

    @ApiAuth(alias = "user:delete")
    @PostMapping("user/delete/{id}")
    public ApiResponse<Boolean> delete (@PathVariable Integer id) {
        boolean res = sxaUsersService.removeById(id);
        if (!res) {
            return ApiResponse.fail("用户删除失败！");
        }

        return ApiResponse.success(res);
    }

    @ApiAuth(alias = "user:page")
    @GetMapping("user/page/{deptId}/{page}/{limit}")
    public ApiResponse<Page<UserListRes>> page (
            @PathVariable Integer deptId,
            @PathVariable long page,
            @PathVariable long limit,
            @RequestParam String name) {
        return ApiResponse.success(sxaUsersService.userList(deptId, page, limit, name));
    }
}

